package com.wsj.a.collapsingdemo;

import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.baidu.mobstat.StatService;
import com.wsj.a.collapsingdemo.adapter.MyVpAdapter;
import com.wsj.a.collapsingdemo.callback.EmptyCallback;
import com.wsj.a.collapsingdemo.callback.ErrorCallback;
import com.wsj.a.collapsingdemo.callback.LoadingCallback;
import com.wsj.a.collapsingdemo.databinding.ActivityAdvertiseBinding;
import com.wsj.a.collapsingdemo.databinding.IncludeAdHeaderBinding;
import com.wsj.a.collapsingdemo.databinding.ItemAdBinding;
import com.wsj.a.collapsingdemo.databinding.ItemFocusBinding;
import com.wsj.a.collapsingdemo.databinding.ItemFocusRecommendBinding;
import com.wsj.a.collapsingdemo.databinding.ItemViewMoreBinding;
import com.wsj.a.collapsingdemo.databinding.ItemWorkBinding;
import com.wsj.a.collapsingdemo.entity.FocusUserEntity;
import com.wsj.a.collapsingdemo.widget.ScalePageTransformer;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.RecyclerViewContract;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.RecyclerViewDelegate;
import com.wsj.a.collapsingdemo.widget.recycler.recycler.StateFulListener;
import com.wsj.library.loadsir.callback.Callback;
import com.wsj.library.loadsir.core.LoadService;
import com.wsj.library.loadsir.core.LoadSir;

import java.util.ArrayList;
import java.util.List;

public class AdvertiseActivity extends AppCompatActivity implements RecyclerViewContract.IFTypeAdapter<FocusUserEntity>,
        RecyclerViewContract.IFLoadData,
        StateFulListener {

    private ActivityAdvertiseBinding mBinding;
    private IncludeAdHeaderBinding mHeadBinding;
    //加载布局
    protected LoadService mLoadService;
    private RecyclerViewDelegate<FocusUserEntity> mRecyclerViewDelegate;
    private List<FocusUserEntity> mData = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this,R.layout.activity_advertise);
        mBinding.includeBaseTitle.setTitle("发现");

        initListView();
        initView();
        initData();
        startFlipping();
        initViewPager();
        initLoadSir();
        getDataInfo();
        initFocusCircleList();


        // 建议在`自定义页面`的页面开始函数中调用
        StatService.onPageStart(AdvertiseActivity.this,"发现页面");
    }

    //加载失败显示占位图
    private void initLoadSir() {
        LoadSir loadSir = new LoadSir.Builder()
                .addCallback(new EmptyCallback())
                .addCallback(new ErrorCallback())
                .addCallback(new LoadingCallback())
                .build();

        mLoadService = loadSir.register(mBinding.contentView, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                mRecyclerViewDelegate.reLoadData();
            }
        });
    }

    public void initListView(){

        mHeadBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.include_ad_header,null,false);

        mRecyclerViewDelegate = RecyclerViewDelegate.with(this, this)
                .recyclerView(mBinding.refreshView, mBinding.recyclerView)
                .setHeaderView(mHeadBinding.getRoot())
                .isShowLoadingView(false)
                .cancelRefresh(false,false)
                .stateFulListener(this)
                .build();
    }

    public void startFlipping(){
        //设置其开始进行view的切换,会循环进行
        mHeadBinding.myVf.startFlipping();
    }

    //设置公告栏条目进出动画
    public void initView(){

        //设置间隔时长(时长应该大于或等于进入和出去播放时间的和)
        mHeadBinding.myVf.setFlipInterval(1300);
        //实现其动画效果
        //1,用java代码写一个进入的动画(有渐变和平移效果)

        AlphaAnimation alpha = new AlphaAnimation(0.0f, 1.0f);
        TranslateAnimation trans = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0F, Animation.RELATIVE_TO_SELF, 0.0f);
        AnimationSet set = new AnimationSet(false);
        set.addAnimation(alpha);
        set.addAnimation(trans);
        set.setDuration(800);//设置作用时长
        //设置进场动画
        mHeadBinding.myVf.setInAnimation(set);

        //用xml写一个离场动画
        mHeadBinding.myVf.setOutAnimation(getApplicationContext(), R.anim.flipper_out);
    }

    public void initData(){
        List<String> data = new ArrayList<>();
        data.add("SOVS 分享了旅行拍照的N种造型");
        data.add("小susu 分享了在厨房中的快乐小事");
        data.add("爱美的小萌妹 分享了美丽摄影的入门技巧");
        data.add("彩虹与邂逅 分享了午后的小惬意");
        mHeadBinding.myVf.setData(data);
    }

    public void initViewPager(){
       List<View> mViews = new ArrayList<>();
       mViews.add(getView(R.drawable.img_1));
       mViews.add(getView(R.drawable.img_2));
       mViews.add(getView(R.drawable.img_3));
       mViews.add(getView(R.drawable.img_4));
       mViews.add(getView(R.drawable.img_5));
       mViews.add(getView(R.drawable.img_6));
       mViews.add(getView(R.drawable.img_7));
       mViews.add(getView(R.drawable.img_8));
       mViews.add(getView(R.drawable.img_9));
       mViews.add(getView(R.drawable.img_10));

        MyVpAdapter myVpAdapter = new MyVpAdapter();
        myVpAdapter.setDatas(mViews);
        mHeadBinding.viewPager.setAdapter(myVpAdapter);
        mHeadBinding.viewPager.setPageTransformer(false,new ScalePageTransformer());
        mHeadBinding.viewPager.setCurrentItem(1);
    }


    public View getView(int res){
        ItemAdBinding itemAdBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_ad,null,false);
        itemAdBinding.image.setImageResource(res);
        return itemAdBinding.getRoot();
    }

    //显示空布局
    @Override
    public void showEmptyView() {
        mLoadService.showCallback(EmptyCallback.class);
    }

    //显示错误布局
    @Override
    public void showErrorView() {
        mLoadService.showCallback(ErrorCallback.class);
    }

    //显示内容布局
    @Override
    public void showContentView() {
        mLoadService.showSuccess();
    }

    //上拉刷新
    @Override
    public void onRefresh() {
        getDataInfo();
    }

    //加载更多
    @Override
    public void onLoadMore() {
       getDataInfo();
    }

    public void getDataInfo(){
        initUserData();
        List<FocusUserEntity> data = new ArrayList<>();
        data.clear();
        data.addAll(mData);
        data.remove(mData.get(mData.size()-1));
        mRecyclerViewDelegate.render(data);
    }

    public void initUserData(){
        mData.add(new FocusUserEntity(R.drawable.avatar_1,"~黑天💗鹅~",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_2,"漫天樱花",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_3,"不归。",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_4,"北崎",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_5,"夏夏天的语",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_6,"萤火之森",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_7,"追梦的女孩",100));
        mData.add(new FocusUserEntity(R.drawable.avatar_8,"星空下的泡沫",100));
        mData.add(new FocusUserEntity());
    }




    public void initFocusCircleList(){
        for(int i=0;i<mData.size();i++){
            if(TextUtils.isEmpty(mData.get(i).nickname)){
                ItemViewMoreBinding itemViewMoreBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_view_more,null,false);
                mHeadBinding.focusCircle.addView(itemViewMoreBinding.getRoot());
            }else{
                ItemFocusBinding itemFocusBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_focus,null,false);
                itemFocusBinding.avatar.setImageResource(mData.get(i).head_img);
                itemFocusBinding.nickname.setText(mData.get(i).nickname);
                itemFocusBinding.fans.setText(mData.get(i).fans+"粉丝");
                itemFocusBinding.btnFocus.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(AdvertiseActivity.this,"关注用户",Toast.LENGTH_SHORT).show();
                        StatService.onEvent(AdvertiseActivity.this,"文艺校友圈列表关注用户", "关注用户",1);
                    }
                });
                mHeadBinding.focusCircle.addView(itemFocusBinding.getRoot());
            }

        }
    }

    @Override
    public void updateView(@NonNull FocusUserEntity data, @NonNull ViewDataBinding binding, int position) {
       ItemFocusRecommendBinding itemFocusRecommendBinding = (ItemFocusRecommendBinding)binding;
       itemFocusRecommendBinding.avatar.setImageResource(data.head_img);
       itemFocusRecommendBinding.nickname.setText("星空下的泡沫");
       itemFocusRecommendBinding.desc.setText("爱吃的美少女丫丫");
       if(itemFocusRecommendBinding.workList.getChildCount()==0){
           addView(itemFocusRecommendBinding.workList);
       }

       itemFocusRecommendBinding.btnFocusOn.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               //点击关注
               // 不指定事件发生次数，默认为1
               Toast.makeText(AdvertiseActivity.this,"关注用户",Toast.LENGTH_SHORT).show();
               StatService.onEvent(AdvertiseActivity.this,"关注文艺校友圈的用户", "AdFocusUserInFriend",1);
           }
       });


    }

    @Override
    public ViewDataBinding createView(ViewGroup parent, int type) {
       return DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_focus_recommend,null,false);
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    public void addView(LinearLayout list){
        list.addView(getWorkItem(R.drawable.img_1,"作品1"));
        list.addView(getWorkItem(R.drawable.img_2,"作品2"));
        list.addView(getWorkItem(R.drawable.img_3,"作品3"));
        list.addView(getWorkItem(R.drawable.img_4,"作品4"));
        list.addView(getWorkItem(R.drawable.img_5,"作品5"));
        list.addView(getViewMoreItem());
    }

    public View getWorkItem(int image,String name){
        ItemWorkBinding itemWorkBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_work,null,false);
        itemWorkBinding.workImage.setImageResource(image);
        itemWorkBinding.workName.setText(name);
        itemWorkBinding.workImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(AdvertiseActivity.this,"",Toast.LENGTH_SHORT).show();
                StatService.onEvent(AdvertiseActivity.this,"点击作品图片", "发现画廊预览",1);
            }
        });
        return itemWorkBinding.getRoot();
    }

    public View getViewMoreItem(){
        ItemViewMoreBinding itemViewMoreBinding = DataBindingUtil.inflate(getLayoutInflater(),R.layout.item_view_more,null,false);
        return itemViewMoreBinding.getRoot();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 建议在`自定义页面`的页面结束函数中调用
        StatService.onPageEnd(AdvertiseActivity.this, "发现页面");
    }
}
